<template>
  <vxe-modal height="90vh" title="新增货架" v-model="hjVisible" width="90%" showFooter show-zoom resize>
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货架名称" prop="hjName">
        <el-input size="mini"
                  v-model="queryParams.hjName"
                  placeholder="请输入货架名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增货架
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['wzgl:hjgl:edit']"-->
      <!--        >修改货架</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除货架
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table height="60vh" :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="hjglList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="仓库名称" align="center" prop="ckName"/>
      <el-table-column label="货架名称" align="center" prop="hjName"/>
      <el-table-column label="货架备注" align="center" prop="hjBz"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="addFc(scope.row)"
          >新增分层
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['wzgl:hjgl:edit']"-->
          <!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货架管理对话框 -->
    <vxe-modal :title="title" v-model="open" width="500px" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="1 === 2" label="仓库管理ID" prop="ckglId">
          <el-input disabled v-model="form.ckglId" placeholder="请输入仓库管理ID"/>
        </el-form-item>
        <el-form-item label="所属仓库" prop="ckName">
          <el-input disabled v-model="form.ckName" placeholder="请输入仓库名称"/>
        </el-form-item>
        <el-form-item label="货架名称" prop="hjName">
          <el-input v-model="form.hjName" placeholder="请输入货架名称"/>
        </el-form-item>
        <el-form-item label="货架备注" prop="hjBz">
          <el-input v-model="form.hjBz" placeholder="请输入货架备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!--    分层管理-->
    <Fcgl ref="fcGl"></Fcgl>
  </vxe-modal>
</template>

<script>
import {addHjgl, delHjgl, getHjgl, listHjgl, updateHjgl} from "@/api/wzgl/hjgl";
import Fcgl from "@/views/wzgl/ckgl/FcGl.vue";

export default {
  name: "Hjgl",
  components: {Fcgl},
  data() {
    return {
      ckGlId: null,//打开后的对应仓库ID
      dyCkList: null, //打开后的对应仓库信息
      //弹窗是否打开
      hjVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货架管理表格数据
      hjglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ckglId: this.ckGlId,
        ckName: null,
        hjName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ckglId: [{required: true, message: "仓库管理ID不能为空", trigger: "blur"}],
        ckName: [{required: true, message: "仓库名称不能为空", trigger: "blur"}],
        hjName: [{required: true, message: "货架名称不能为空", trigger: "blur"}],
      },
    };
  },
  created() {

  },
  methods: {
    /** 自定义编号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    // 新增分层
    addFc(row) {
      // console.log(row,'1111111111111111')
      this.$refs.fcGl.handleOpen(row)
    },
    //打开
    handleOpen(row) {
      this.dyCkList = row //对应仓库信息
      this.ckGlId = row.ckglId
      this.hjVisible = true
      this.getList()
    },
    /** 查询货架管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.ckglId = this.ckGlId
      listHjgl(this.queryParams).then(response => {
        this.hjglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hjglId: null,
        ckglId: this.dyCkList.ckglId,
        ckName: this.dyCkList.ckName,
        hjName: null,
        hjBz: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hjglId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      console.log(this.form, '表单信息')
      this.open = true;
      this.title = "添加货架管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hjglId = row.hjglId || this.ids
      getHjgl(hjglId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货架管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.hjglId != null) {
            updateHjgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHjgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hjglIds = row.hjglId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delHjgl(hjglIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
